﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows.Controls;
using Microsoft.Practices.Composite.Regions;

namespace MefBasic.Helper
{
    [Export]
    public class RegionManagerHelper
    {
        [Import]
        private Lazy<IRegionManager> RegionManager { get; set; }

        public void ActivateView<T>(string regionName)
        {
            var region = RegionManager.Value.Regions[regionName];
            var view = region.Views.Cast<IView>().SingleOrDefault(v => v is T);
            if(view!=null)
                region.Activate(view);
        }

        public double ViewWidth(string regionName)
        {
            var region = RegionManager.Value.Regions[regionName];
            return region.ActiveViews.Cast<UserControl>().Sum(activeView => activeView.ActualWidth);
        }
        public double ViewHeight(string regionName)
        {
            var region = RegionManager.Value.Regions[regionName];
            return region.ActiveViews.Cast<UserControl>().Sum(activeView => activeView.ActualHeight);
        }
    }
}
